package com.lyq.permission.controller;

import com.lyq.annotation.LYQPermission;
import com.lyq.permission.domain.Permission;
import com.lyq.permission.query.PermissionQuery;
import com.lyq.permission.service.IPermissionService;
import com.lyq.basic.utils.PageList;
import com.lyq.basic.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:liangyunqian
 * @Date: 2022/12/13  22:07
 * @Description:
 */
@Api(value = "权限管理的api",description = "权限的查询")
@LYQPermission(name = "权限管理")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @ApiOperation("分页查询")
    @LYQPermission(name = "分页查询")
    @PostMapping
    public R listPage(@RequestBody PermissionQuery query){
        try {
            PageList<Permission> pageList = permissionService.listPage(query);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    @ApiOperation("查询权限树")
    @LYQPermission(name = "查询权限树")
    @GetMapping("/tree")
    public R tree(){
        try {
            List<Permission> permissions =  permissionService.tree();
            return R.me().setData(permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询权限树失败!");
        }
    }

}
